package com.hj.saapp.common.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.hj.saapp.BCDApplication;

import java.math.BigDecimal;
import java.util.List;

/**
 * 
 * UI相关工具类
 * 
 * @author wangwei
 * @version [1.0, 2014-12-5]
 * @see [相关类/方法]
 */
public class DisplayUtils
{
    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     * 
     * @param pxValue

     * @return
     */
    public static int px2dip(Context context, float pxValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int)(pxValue / scale + 0.5f);
    }
    
    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     * 
     * @param dipValue

     * @return
     */
    public static int dip2px( float dipValue)
    {
        final float scale = BCDApplication.getApp().getResources().getDisplayMetrics().density;
        return (int)(dipValue * scale + 0.5f);
    }
    
    /**
     * 获得屏幕高度
     * 

     * @return
     */
    public static int getScreenWidth()
    {
        WindowManager wm = (WindowManager)BCDApplication.getApp().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
    
    /**
     * 获得屏幕高度
     * 

     * @return
     */
    public static int getScreenHeight()
    {
        WindowManager wm = (WindowManager)BCDApplication.getApp().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }
    
    /**
     * 获得状态栏的高度
     * 
     * @param context
     * @return
     */
    public static int getStatusHeight(Context context)
    {
        
        int statusHeight = -1;
        try
        {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return statusHeight;
    }
    
    public static double getDecimal(double num)
    {
        if (Double.isNaN(num))
        {
            return 0;
        }
        BigDecimal bd = new BigDecimal(num);
        num = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return num;
    }
    
    public static boolean isAvilible(Context context)
    {
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        return pinfo.size() > 1;
    }
    
    /**
     * 将px值转换为sp值，保证文字大小不变
     * 
     * @param pxValue
     * @return
     */
    public static int px2sp(Context context, float pxValue)
    {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int)(pxValue / fontScale + 0.5f);
    }
    
    /**
     * 将sp值转换为px值，保证文字大小不变
     * 
     * @param spValue
     * @return
     */
    public static int sp2px( float spValue)
    {
        final float fontScale = BCDApplication.getApp().getResources().getDisplayMetrics().scaledDensity;
        return (int)(spValue * fontScale + 0.5f);
    }
    
    /**
     * 
     * 获取view height
     * 
     * @param view
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static int getHeight(View view)
    {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return (view.getMeasuredHeight());
    }
    
    /**
     * 
     * 设置控件所在的位置XY，并且不改变宽高，XY为绝对位置
     * 
     * @param parentView 父类layout
     * @param view 想显示的view
     * @param x 父类x坐标
     * @param y 父类y坐标
     * @see [类、类#方法、类#成员]
     */
    public static void setLayoutXY(RelativeLayout parentView, View view, int x, int y)
    {
        MarginLayoutParams margin = new MarginLayoutParams(view.getLayoutParams());
        margin.setMargins(x, y, x + margin.width, y + margin.height);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
        view.setLayoutParams(layoutParams);
        parentView.addView(view);
    }

}
